package top.zhouze.common.base;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import androidx.lifecycle.*;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import lombok.Getter;
import lombok.Setter;

/**
 * @author 周仄
 * @since 2023/6/26 17:12
 */
public class BaseViewModel extends ViewModel {
    @Getter
    @Setter
    private LifecycleOwner lifecycleOwner;
    @Getter
    private final MutableLiveData<Boolean> finishEvent = new MutableLiveData<>();
    @Getter
    private final MutableLiveData<String> toastEvent = new MutableLiveData<>();
    private CompositeDisposable mCompositeDisposable;


    public void startActivity(View view, Class<? extends Activity> clz) {
        Context context = view.getContext();
        Intent intent = new Intent(context,clz);
        context.startActivity(intent);
    }

    public void startActivity(View view, Class<?> clz, Bundle bundle) {
        Context context = view.getContext();
        Intent intent = new Intent(context,clz);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    public void finish(View view) {
        finishEvent.postValue(true);
    }

    public void showToast(String str) {
        toastEvent.postValue(str);
    }

    protected void addSubscribe(Disposable disposable) {
        if (mCompositeDisposable == null) {
            mCompositeDisposable = new CompositeDisposable();
        }
        if (disposable != null) {
            mCompositeDisposable.add(disposable);
        }
    }

    @Override
    protected void onCleared() {
        super.onCleared();
        if (mCompositeDisposable != null && !mCompositeDisposable.isDisposed()) {
            mCompositeDisposable.dispose();
        }
    }

}
